var exp = require('express')
var fs = require('fs')
const { address, item, json1, User, img, car } = require('../lib/mongodb')

var app = exp.Router()
app.get('/', (req, res) => {
    var hs = fs.readFileSync('www/json/resource_goods.json', 'utf-8')
    res.render('index', { data: JSON.parse(hs) })
})
app.get('/news', (req, res) => {
    json1.find((err, data) => {
        data = JSON.stringify(data[0].data)
        data = JSON.parse(data)
        console.log(data);
        res.render('news', { data })
    })

})
app.get('/haitao', (req, res) => {
    var hs = fs.readFileSync('www/json/hub.json')
    res.render('haitao', { data: JSON.parse(hs) })
})
app.get('/search', (req, res) => {
    res.render('search')
})
app.get('/member', (req, res) => {
    img.findOne({
        iduser: req.cookies.phone
    }, (err, data) => {
        res.render('member', { data })
    })

})
app.get('/address', (req, res) => {
    address.find({
        phone1: req.cookies.phone
    }, (err, data) => {
        res.render('address', { data })
    })

})
app.get('/elsegroup.html', (req, res) => {
    res.render('elsegroup')
})
app.get('/order', (req, res) => {
    address.findOne({
        _id: req.url.split('id=')[1]
    }, (err, data) => {
        res.render('order', { data: JSON.parse(req.cookies.ping), add: data })
    })

})
app.get('/detail', (req, res) => {
    // var hs = fs.readFileSync('www/json/resource_goods.json', 'utf-8')
    // JSON.parse(hs).goods_list.forEach(item => {
    //     if (item.goods_id == req.originalUrl.split('id=')[1]) {
    //         res.render('detail', { item })
    //     }
    // })
    json1.find({}, (err, data) => {
        data = data[1].goods_list
        data = JSON.stringify(data)
        data = JSON.parse(data)
        data.forEach(item => {
            if (item.goods_id == req.originalUrl.split('id=')[1]) {
                res.render('detail', { item })
            }
        })
    })

})
app.get('/small', (req, res) => {
    res.render('small')
})
app.get('/login', (req, res) => {
    res.render('login')
})
app.get('/register', (req, res) => {
    res.render('register')
})
app.get('/kefu', (req, res) => {
    res.render('kefu')
})
app.get('/add_address', (req, res) => {
    var data = req.originalUrl.split('id=')[1]
    address.findOne({
        _id: data
    }, (err, data) => {
        res.render('add_address', { data })
    })
})
app.get('/pay', (req, res) => {
    item.find({
        user: req.cookies.phone
    }, (err, data) => {
        var data1 = data.filter(function (item, i) {
            if (item.pay == '已付款') {
                return item
            }
        })
        var data2 = data.filter(function (item, i) {
            if (item.pay == '未付款') {
                return item
            }
        })
        res.render('pay', { data: data.reverse(), data1: data1.reverse(), data2: data2.reverse() })
    })

})
app.get('/zhifu', (req, res) => {
    res.render('zhifu')
})
app.get('/my', (req, res) => {
    User.findOne(
        { _id: req.cookies.phone },
        (err, data) => {
            // console.log(data);
            res.render('my', { data })
        }
    )

})
app.get('/theme1', (req, res) => {
    // console.log(req.query.keyvalue);
    json1.find({}, (err, data) => {
        data = data[1].goods_list
        data = JSON.stringify(data)
        data = JSON.parse(data)
        var hs = data.filter(item => {
            if (item.goods_name.includes(req.query.keyvalue)) {
                return data
            }
        })
        // console.log(hs);
        res.render('theme1', { hs })
    })

})
app.get('/img', (req, res) => {
    res.render('img')
})
app.get('/Car', (req, res) => {
    car.find({
        user: req.cookies.phone
    }, (er, da) => {
        res.render('Car', { da: da.reverse() })
    })

})
module.exports = app